package cn.com.xinjinfu.pdoulcProduct.activity.recharge;

import android.content.Intent;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONObject;
import java.util.Map;
import cn.com.xinjinfu.pdoulcProduct.R;
import cn.com.xinjinfu.pdoulcProduct.activity.base.BaseActivity;
import cn.com.xinjinfu.pdoulcProduct.engine.ComAsk;
import cn.com.xinjinfu.pdoulcProduct.engine.DataHandler;
import cn.com.xinjinfu.pdoulcProduct.entity.User;
import cn.com.xinjinfu.pdoulcProduct.manager.AccountManager;
import cn.com.xinjinfu.pdoulcProduct.manager.ActivityManager;
import cn.com.xinjinfu.pdoulcProduct.manager.JSONManager;
import cn.com.xinjinfu.pdoulcProduct.manager.L;
import cn.com.xinjinfu.pdoulcProduct.manager.TitleManager;
import cn.com.xinjinfu.pdoulcProduct.utils.NewUtil;

/**
 * 忘记密码
 * Created by android-adt on 2016/1/25.
 */
public class ForgetPayPwdActivity extends BaseActivity implements View.OnClickListener {
    private String mobile;
    private TextView tv_error_hint;
    private EditText et_person_id,etcode;
    private Button btcode;
    private Button pay_confirm_bt;
    private RequestQueue requen;
    private String personId,code,userId;
    private User user;
    private GradientDrawable myGrad;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_forget_paypwd);
        TitleManager.showTitle(this, "重置交易密码", "");
        ActivityManager.getInstance().addActivity(this);
        user = AccountManager.getInstance().getUser();
        mobile = user.getCurrUser().getMobile();
        userId = user.getCurrUser().getIdNumber();
        intiViews();
        requen = Volley.newRequestQueue(this);
    }
    protected void intiViews() {
        tv_error_hint = (TextView) findViewById(R.id.tv_error_hint);
        et_person_id=(EditText)findViewById(R.id.et_person_id);
        etcode=(EditText)findViewById(R.id.et_code);//验证码
        et_person_id.addTextChangedListener(mTextWatcher);

        btcode=(Button)findViewById(R.id.bt_code);  //获得验证码
        btcode.setOnClickListener(this);
        btcode.setEnabled(false);
        myGrad = (GradientDrawable)btcode.getBackground();
        myGrad.setColor(0xFFCCCCCC);
        pay_confirm_bt=(Button)findViewById(R.id.pay_confirm_bt);
        pay_confirm_bt.setOnClickListener(this);
    }

    public void onClick(View v) {
        if (!NewUtil.checkClick(v.getId())) return;
        switch (v.getId()) {
            case R.id.bt_code:
                ComAsk.getVerifyCode(mobile, this, btcode,"2");
                break;
            case R.id.pay_confirm_bt:
                loginRequen();
                break;
        }
    }


    public void loginRequen(){
        code = etcode.getText().toString();
        final Map<String, String> parameters = DataHandler.getParameters("2113");
        User user = AccountManager.getInstance().getUser();
        if (user != null) {
            parameters.put("id", user.getCurrUser().getSign());
        }
        parameters.put("code", code);
        parameters.put("idNumber", personId);

        JsonObjectRequest request = new JsonObjectRequest(DataHandler.getBuildUrl(parameters), null, succeedRespon, error);
        requen.add(request);
    }

    private Response.ErrorListener error = new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            DataHandler.errorHandler(ForgetPayPwdActivity.this, error);
        }
    };

    private Response.Listener<JSONObject> succeedRespon = new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject jobj) {
            L.i("忘记密码" + jobj.toString());
            if(JSONManager.getError(jobj) == -1){
                Intent intent = new Intent();
                intent.putExtra("falg", 0);
                intent.setClass(ForgetPayPwdActivity.this, TradePWD.class);
                startActivity(intent);
            }else{
                tv_error_hint.setText(JSONManager.getMsg(jobj));
            }

        }
    };
    TextWatcher mTextWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int arg1, int arg2,
                                      int arg3) {
        }
        @Override
        public void onTextChanged(CharSequence s, int arg1, int arg2,
                                  int arg3) {
        }
        @Override
        public void afterTextChanged(Editable s) {
            personId = et_person_id.getText().toString();
            code = etcode.getText().toString();
            tv_error_hint.setText("");
            if (personId.equals(userId)) {
                myGrad.setColor(0xFF6ED3D6);
                btcode.setEnabled(true);
                pay_confirm_bt.setEnabled(true);
            }else {
                myGrad.setColor(0xFFCCCCCC);
                btcode.setEnabled(false);
                pay_confirm_bt.setEnabled(false);
            }
        }
    };

}
